package consultan.vanke.com.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import consultan.vanke.com.R;
import consultan.vanke.com.adapter.BaseFragmentAdapter;
import consultan.vanke.com.adapter.CommonAdapter;
import consultan.vanke.com.adapter.ViewHolder;
import consultan.vanke.com.bean.HomeJumpBean;
import consultan.vanke.com.constant.ConstantLibrary;
import consultan.vanke.com.event.MessageEvent;
import consultan.vanke.com.utils.SimpleToolbar;
import me.yokeyword.fragmentation.SupportFragment;

import static com.bytedance.sdk.account.open.aweme.utils.ViewUtils.getStatusBarHeight;
import static consultan.vanke.com.utils.StatusBarUtil.setPaddingTop;
import static consultan.vanke.com.utils.StatusBarUtil.setRootView;

public class ShangchengFrg extends BaseFragment {
    @BindView(R.id.simple_toolbar)
    SimpleToolbar simpleToolbar;
    @BindView(R.id.simple_toolbar_main)
    ConstraintLayout simpleToolbarMain;
    @BindView(R.id.tablayout)
    XTabLayout tablayout;
    @BindView(R.id.vp_view)
    ViewPager vpView;
    @BindView(R.id.rel_top)
    LinearLayout relTop;
    @BindView(R.id.shadow)
    TextView shadow;
    @BindView(R.id.drag_point_view)
    TextView dragPointView;
    @BindView(R.id.linear_left)
    LinearLayout linearLeft;
    @BindView(R.id.rel_bottom)
    RelativeLayout relBottom;
    @BindView(R.id.lienar_bottom)
    LinearLayout lienarBottom;
    @BindView(R.id.rel_all)
    RelativeLayout relAll;
    @BindView(R.id.tv_jiesuan)
    TextView tvJiesuan;
    private View inflate;
    private ArrayList<String> titles;
    private ArrayList<BaseFragment> fragments;
    private ShopListFragment shopListFragment;
    PopupWindow popupWindow, popupWindowlist;
    View def_sort, def_list;
    private static ShangchengFrg shangchengFrg;
    private RecyclerView recyclerView;
    private CommonAdapter<String> adapterCenter;
    private List<String> subjectsBeansCenter = new ArrayList<>();

    public static SupportFragment showis() {

        return shangchengFrg = new ShangchengFrg();
    }


    @Override
    protected void initViews() {
        simpleToolbar.setMainBg(getResources().getColor(R.color.white_color));
        simpleToolbar.setMainTitle("商城");
        titles = new ArrayList<>();
        titles.add("烟酒");
        titles.add("香烟");
        titles.add("茗茶");
        titles.add("饮料");
        titles.add("零食");
        titles.add("速食品");
        titles.add("休闲食品");
        fragments = new ArrayList<>();
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        fragments.add(new ShopListhomeFragment(shangchengFrg));
        BaseFragmentAdapter adapter = new BaseFragmentAdapter(getChildFragmentManager(), fragments, titles);
        vpView.setAdapter(adapter);
        vpView.setOffscreenPageLimit(ConstantLibrary.VIEWPAGERLIMIT);
        tablayout.setupWithViewPager(vpView);
        def_sort = LayoutInflater.from(_mActivity).inflate(R.layout.sc_sort_def, null);
        popupWindow = new PopupWindow(def_sort, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口

        def_list = LayoutInflater.from(_mActivity).inflate(R.layout.sc_sort_list, null);
        recyclerView = def_list.findViewById(R.id.recy_list);
        for (int i = 0; i < 2; i++) {
            subjectsBeansCenter.add("111");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        adapterCenter = new CommonAdapter<String>(_mActivity, subjectsBeansCenter, R.layout.shop_items) {
            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            }

            @Override
            public void convert(ViewHolder holder, String assetBean) {
//                ImageView img_photo = holder.getView(R.id.img_photo);
            }

        };
        recyclerView.setAdapter(adapterCenter);
        popupWindowlist = new PopupWindow(def_list, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindowlist.setOutsideTouchable(true);
        popupWindowlist.setBackgroundDrawable(new BitmapDrawable());
        popupWindowlist.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
    }

    @Override
    protected void initDatas() {
        simpleToolbar.setLeftTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
    }

    @Override
    protected void initEvents() {
        tablayout.addOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {
            }
        });
        vpView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected View initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = View.inflate(_mActivity, R.layout.shangcheng_fragment, null);
        return inflate;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        ViewGroup decorView = (ViewGroup) _mActivity.getWindow().getDecorView();
        View fakeStatusBarView = new View(_mActivity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(_mActivity) + 3);
        fakeStatusBarView.setLayoutParams(params);
        fakeStatusBarView.setBackground(getResources().getDrawable(R.drawable.bg_white));
        fakeStatusBarView.setId(android.R.id.custom);
        decorView.addView(fakeStatusBarView);
        setRootView(_mActivity, false);
        setStatusBarColors(getResources().getColor(R.color.white_color), true);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @SuppressLint("WrongConstant")
    public void showPop() {
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
            return;
        }
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
//                imgNorsort.setBackgroundDrawable(getResources().getDrawable(R.mipmap.asset_buttom));
            }
        });
        popupWindow.showAtLocation(inflate.findViewById(R.id.rel_all), Gravity.BOTTOM, 0, 0);
    }

    @OnClick({R.id.linear_left, R.id.tv_jiesuan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_left:
                if (popupWindowlist.isShowing()) {
                    popupWindowlist.dismiss();
                    return;
                }
                popupWindowlist.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
//                imgNorsort.setBackgroundDrawable(getResources().getDrawable(R.mipmap.asset_buttom));
                    }
                });
                popupWindowlist.showAtLocation(inflate.findViewById(R.id.rel_all), Gravity.BOTTOM, 0, 0);
                break;
            case R.id.tv_jiesuan:
                MessageEvent<HomeJumpBean> messageEvent = new MessageEvent<>();
                messageEvent.setData(new HomeJumpBean(ConstantLibrary.jiesuanFragment, ConstantLibrary.TYPE_TIME_OUT));
                EventBus.getDefault().post(messageEvent);
                break;
        }
    }
}
